// pages/myAttention/myAttention.js
import api from '../../utils/api.js'
var app = getApp();
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        size: 10,
        attention: [],
        none: false,
        current: 0,
        imgUrl: config.imgUrl,
    },

    // 获得用户关注的主题接口
    getUserProjects(obj) {
        http.request({
            url: api.getUserProjects,
            data: obj,
            success: res => {
                console.log(res)
                if (res.msg == "你还没有收藏专题哦 快去收藏吧0.0") {
                    this.setData({
                        none: true,
                        attention: []
                    })
                } else {
                    var attention = res.data
                    attention.map(item => {
                        item.flag = true
                    })
                    this.setData({
                        attention
                    })
                }
            }
        })
    },
    // 拿到用户收藏的厨师列表
    getUserCollectJyqcList(obj) {
        http.request({
            url: api.getUsershoucangCollectList,
            data: obj,
            success: res => {
                console.log(res)
                var chefList = res
                chefList.map(item => {
                    item.flag = true
                })
                this.setData({
                    chefList
                })
            }
        })
    },
    // 用户对专题添加或取消收藏专题接口
    addDelCollectProject(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelCollectProject,
                data: obj,
                success: res => {
                    if (res.code == 200) {
                        resolve(res)
                    }
                }
            })
        })
    },

    // 用户关注与取消关注一位厨师接口
    addDelJyqc(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelJyqC,
                data: obj,
                success: res => {
                    if (res.code == 200) {
                        resolve(res)
                    }
                }
            })
        })
    },

    //取消对专题的关注
    quxiao1(e) {
        var flag = e.target.dataset.flag
        let obj = {
            userId: wx.getStorageSync("user_id"),
            productId: e.target.dataset.id,
            statusID: 1
        }
        if (flag) {
            obj.statusID = 2
        }
        this.addDelCollectProject(obj).then(res => {
            var attention = this.data.attention
            var item = attention.filter(item => {
                return item.project_id == e.target.dataset.id
            })[0]
            item.flag = !item.flag
            this.setData({
                attention
            })
            var msg = "取消成功"
            if (item.flag) {
                msg = "关注成功"
            }
            wx.showToast({
                title: msg,
                duration: 1500
            })
        })
    },
    //取消对厨师的关注
    quxiao2(e) {
        var flag = e.target.dataset.flag
        let obj = {
            user_id: wx.getStorageSync("user_id"),
            jyqc_id: e.target.dataset.id,
            status_id: 1
        }
        if (flag) {
            obj.status_id = 2
        }
        this.addDelJyqc(obj).then(res => {
            var chefList = this.data.chefList
            var item = chefList.filter(item => {
                return item.jyqc_id == e.target.dataset.id
            })[0]
            item.flag = !item.flag
            this.setData({
                chefList
            })
            var msg = "取消成功"
            if (item.flag) {
                msg = "关注成功"
            }
            wx.showToast({
                title: msg,
                duration: 1500
            })
        })
    },

    //跳转首页专题
    toIndex(e) {
        app.globalData.id = e.currentTarget.dataset.id
        console.log(e.currentTarget.dataset.id)
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    toProject(e) { // 跳转专题页面
        wx.navigateTo({
            url: "/pages/project/project?id=" + e.currentTarget.dataset.id
        })
    },
    toChef(e) { // 跳转厨师页面
        wx.navigateTo({
            url: "/pages/chef/chef?id=" + e.currentTarget.dataset.id
        })
    },
    change(e) {
        this.setData({
            current: e.currentTarget.dataset.idx
        })
        if (e.currentTarget.dataset.idx == 0) {
            if (this.data.attention.length != 0) {
                this.setData({
                    none: false
                })
            } else {
                this.setData({
                    none: true
                })
            }
        } else {
            if (this.data.chefList.length != 0) {
                this.setData({
                    none: false
                })
            } else {
                this.setData({
                    none: true
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let obj = {
            uid: wx.getStorageSync("user_id"),
            page: 1,
            size: this.data.size
        }
        this.getUserProjects(obj) // 获得用户关注的主题接口
        this.getUserCollectJyqcList({ id: wx.getStorageSync("user_id") }) // 拿到用户收藏的厨师列表
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})